import * as actionTypes from "./constants";
import { 
  getTopBanners,
  getHotRecommends,
  getNewAlbums,
  getTopLists,
  getArtistLists
 } from "@/services/recommend";

const changeTopBannerAction = (res) => ({
  type: actionTypes.CHANGE_TOP_BANNERS,
  topBanners: res.banners,
});

const changeHotRecommendAction = (res) => ({
  type: actionTypes.CHANGE_HOT_RECOMMEND,
  hotRecommends: res.result,
})

const changeNewAlbumAction = res => ({
  type: actionTypes.CHANGE_NEW_ALBUM,
  newAlbums: res.albums,
})

const changeUpRankingAction = res => ({
  type: actionTypes.CHANGE_UP_RANKING,
  topUpLists: res.playlist
})

const changeNewRankingAction = res => ({
  type: actionTypes.CHANGE_NEW_RANKING,
  topNewLists: res.playlist
})

const changeOriginRankingAction = res => ({
  type: actionTypes.CHANGE_ORIGIN_RANKING,
  topOriginLists: res.playlist
})

const changeSettleSingersAction = res => ({
  type: actionTypes.CHANGE_SETTLE_SINGERS,
  settleSingers: res.artists
})


// 为什么不把外面那个函数传进去呢，因为可能需要依赖一些参数
export const getTopBannerAction = () => {
  // 返回的这个函数本身有一个参数 dispatch
  return (dispatch) => {
    getTopBanners().then((res) => {
      dispatch(changeTopBannerAction(res));
    });
  };
};

export const getHotRecommendAction = (limit) => {
  return dispatch => {
    getHotRecommends(limit).then(res => {
      dispatch(changeHotRecommendAction(res))
    })
  }
}

export const getNewAlbumAction = (limit, offset) => {
  return dispatch => {
    getNewAlbums(limit, offset).then(res => {
      dispatch(changeNewAlbumAction(res))
    })
  }
}

export const getTopListAction = (idx) => {
  return dispatch => {
    getTopLists(idx).then(res => {
      switch (idx) {
        case 0:
          dispatch(changeUpRankingAction(res));
          break;
        case 2:
          dispatch(changeNewRankingAction(res));
          break;
        case 3:
          dispatch(changeOriginRankingAction(res));
          break;
        default:
      }
    })
  }
}

export const getArtistListsAction = () => {
  return dispatch => {
    getArtistLists(5, 5001).then(res => {
      dispatch(changeSettleSingersAction(res));
    })
  }
}
